﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
using SignalR.Hosting.AspNet.Routing;
using SignalR;

namespace HIS {

    public class Global : System.Web.HttpApplication {

        public static Dictionary<string, string> ServiceUriDictionary = new Dictionary<string, string>();

        void Application_Start(object sender, EventArgs e) {
            RouteTable.Routes.Ignore("{*alljs}", new { allcss = @".*\.js(/.*)?" });
            RouteTable.Routes.Ignore("{*allcss}", new { allcss = @".*\.css(/.*)?" });
            RouteTable.Routes.Ignore("{*allaspx}", new { allcss = @".*\.aspx(/.*)?" });
            RouteTable.Routes.Ignore("{*allpng}", new { allcss = @".*\.png(/.*)?" });
            RouteTable.Routes.Ignore("{*alljpg}", new { allcss = @".*\.jpg(/.*)?" });
            RouteTable.Routes.Ignore("{*allgif}", new { allcss = @".*\.gif(/.*)?" });
            RouteTable.Routes.Ignore("{*allwof}", new { allcss = @".*\.wof(/.*)?" });
            RouteTable.Routes.Ignore("{*allttf}", new { allcss = @".*\.ttf(/.*)?" });
            RouteTable.Routes.Ignore("{*allsvg}", new { allcss = @".*\.svg(/.*)?" });
            RouteTable.Routes.Ignore("{*alleot}", new { allcss = @".*\.eot(/.*)?" });
            RouteTable.Routes.Ignore("{*allaspx}", new { allcss = @".*\.aspx(/.*)?" });
            RouteTable.Routes.Ignore("{*allashx}", new { allcss = @".*\.ashx(/.*)?" });

            RouteTable.Routes.Ignore("Images/{*pathInfo}");
            RouteTable.Routes.Ignore("Scripts/{*pathInfo}");
            RouteTable.Routes.Ignore("Styles/{*pathInfo}");
            RouteTable.Routes.Ignore("signalr/{*pathInfo}");

            //Default Page
            RouteTable.Routes.MapPageRoute("Default1", "", "~/invoker.aspx");
            RouteTable.Routes.MapPageRoute("ParamType1", "{par1}/", "~/invoker.aspx");
            RouteTable.Routes.MapPageRoute("ParamType2", "{par1}/{par2}/", "~/invoker.aspx");
            RouteTable.Routes.MapPageRoute("ParamType3", "{par1}/{par2}/{par3}/", "~/invoker.aspx");
            RouteTable.Routes.MapPageRoute("ParamType4", "{par1}/{par2}/{par3}/{par4}/", "~/invoker.aspx");

            SignalR.Hosting.AspNet.AspNetHost.DependencyResolver.Register(typeof(IConnectionIdFactory), () => new HIS.Push.HubClientIdFactory());
            SetServiceUrl();
        }

        void Application_End(object sender, EventArgs e) {
        }

        void Application_Error(object sender, EventArgs e) {
        }

        void Session_Start(object sender, EventArgs e) {
        }

        void Session_End(object sender, EventArgs e) {

        }

        private void SetServiceUrl() {
            string filePath = HttpContext.Current.Server.MapPath("~/service.ini");
            string[] lines = System.IO.File.ReadAllLines(filePath);

            for (int i = 0; i < lines.Length; i++) {
                try {
                    string[] splitLine = lines[i].Split('|');
                    ServiceUriDictionary.Add(splitLine[0], splitLine[1]);
                } catch {
                }
            }
        }

    }
}
